package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import model.User;
import model.Users;
import dataLayer.DAOUser;
import dataLayer.DBconnect;
import types.ConsoleType;
import view.PanelHolder;
import view.PanelUserList;
import view.Panelregister;

public class AddUser extends DBconnect implements ActionListener {

	private static AddUser single;
	private Panelregister Rform;

	public String User_Name;

	public char[] Password;
	public char[] PasswordConfirm;
	public String stringpassword;
	public String stringconfirmpassword;

	public AddUser(Panelregister Rform) {
		this.Rform = Rform;
	}

	// Check on fields for addTeacher and Error message in console
	public void actionPerformed(ActionEvent e) {

		openConn();
		String message = null;
		ConsoleType type = null;

		User_Name = Rform.getNameField().getText();
		Password = Rform.getPasswordField().getPassword();
		PasswordConfirm = Rform.getConfirmPassField().getPassword();

		stringpassword = new String(Password);

		stringconfirmpassword = new String(PasswordConfirm);

		User u = new User(User_Name, stringpassword);

		if (e.getActionCommand().equals("Opslaan")) {

			if (User_Name.equals("")) {
				message = "Vul een gebruikersnaam in";
				type = ConsoleType.EMPTY;
			} else if (Password.length == 0 && PasswordConfirm.length == 0) {
				message = "Vul de wachtwoorden in";
				type = ConsoleType.EMPTY;
			} else if (Password.length == 0) {
				message = "Vul een wachtwoord in";
				type = ConsoleType.EMPTY;
			} else if (PasswordConfirm.length == 0) {
				message = "Bevestig het wachtwoord";
				type = ConsoleType.EMPTY;
			} else if (!(stringconfirmpassword.equals(stringpassword))) {
				message = "De wachtwoorden komen niet overeen";
				type = ConsoleType.EMPTY;
			} else if (stringpassword.contains(" ")) {
				message = "Het wachtwoord mag geen spaties bevatten";
				type = ConsoleType.EMPTY;
			} else if (!(Users.getInstance().addUser(u))) {
				message = "Er bestaat al een gebruiker met deze naam";
				type = ConsoleType.ERROR;
			} else {
				// adds the user to the database
				DAOUser DAU = DAOUser.getInstance();
				DAU.insertUser(u);

				PanelHolder ph = PanelHolder.getInstance();
				ph.remove(Rform);
				ph.add(PanelUserList.getInstance());
				ph.repaint();
				ph.revalidate();

				message = "Gebruiker " + User_Name + " is toegevoegd.";
				type = ConsoleType.SUCCESS;
			}
		}

		Console.getInstance().setConsole(message, type);
	}

	public Panelregister getRform() {
		return Rform;
	}

	public void setRform(Panelregister rform) {
		Rform = rform;
	}

	public String getUser_Name() {
		return User_Name;
	}

	public void setUser_Name(String user_Name) {
		User_Name = user_Name;
	}

	public char[] getPassword() {
		return Password;
	}

	public void setPassword(char[] password) {
		Password = password;
	}

	public char[] getPasswordConfirm() {
		return PasswordConfirm;
	}

	public void setPasswordConfirm(char[] passwordConfirm) {
		PasswordConfirm = passwordConfirm;
	}

	public String getStringPassword() {
		return stringpassword;
	}

	public static AddUser getInstance() {
		if (single == null) {
			single = new AddUser(null);
		}
		return single;
	}

}